//
//  ARTWatchHistorySleepTopView.m
//  ChartsDemo
//
//  Created by 刘权 on 2021/10/13.
//

#import "ARTWatchHistorySleepTopView.h"

@implementation ARTWatchHistorySleepTopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-  (instancetype)init{
    self = [super init];
    if (!self) {
        return nil;
    }
    // init code
    NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"ARTWatchHistorySleepTopView" owner:nil options:nil];
    // 加载nib
    self = [arrayOfViews objectAtIndex:0];
    self.backgroundColor = [UIColor whiteColor];
    self.frame = J_Frame(15, 0, J_SCREEN_WIDTH-15*2, _bottomView.v_MaxY+20);
    
    J_ViewRadius(self, 10);
    
    _detailLabel.displaysAsynchronously = YES;
    _detailLabel.numberOfLines = 0;
    
    _ins1Label.text = FYLocalizedString(@"oxygen_normal", @"");
    _ins2Label.text = FYLocalizedString(@"oxygen_light", @"");
    _ins3Label.text = FYLocalizedString(@"oxygen_moderate", @"");
    _ins4Label.text = FYLocalizedString(@"oxygen_severe", @"");
    return self;
}

- (void)setHisModel:(ARTWatchHisModel *)hisModel{
    _hisModel = hisModel;
    _titleLabel.text = [NSString stringWithFormat:@"%@：",FYLocalizedString(@"sleep_watch_knowledge_title8", @"")];
    NSString *date = hisModel.date;
    if ([date containsString:@" "]) {
        date = [date componentsSeparatedByString:@" "].firstObject;
    }
    _dateLabel.text = date;
    
    NSDictionary *commonDic = @{NSFontAttributeName:J_Font(14),NSForegroundColorAttributeName:J_ColorFromHEX(0x999999)};
    NSDictionary *keyDic = @{};
    if (hisModel.odi < 10) {
        keyDic = @{NSFontAttributeName:J_Font(14),NSForegroundColorAttributeName:J_ColorFromHEX(0x0DA462)};
    }else if (hisModel.odi < 20){
        keyDic = @{NSFontAttributeName:J_Font(14),NSForegroundColorAttributeName:J_ColorFromHEX(0xFFE84A)};
    }else if (hisModel.odi < 30){
        keyDic = @{NSFontAttributeName:J_Font(14),NSForegroundColorAttributeName:J_ColorFromHEX(0xFF9627)};
    }else{
        keyDic = @{NSFontAttributeName:J_Font(14),NSForegroundColorAttributeName:J_ColorFromHEX(0xE50C0C)};
    }
    
//    NSString *message = FYLocalizedString(@"oxygen_healthy", @"");
//    switch (hisModel.testResult){
//        case 64:
//            message = FYLocalizedString(@"oxygen_healthy", @"");
//            break;
//        case 65:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips2", @"");
//            break;
//        case 66:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips4", @"");
//            break;
//        case 67:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips1", @"");
//            break;
//        case 68:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips3", @"");
//            break;
//        case 69:
//            message = FYLocalizedString(@"sleep_watch_appraise_low", @"");
//            break;
//        case 70:
//            message = FYLocalizedString(@"sleep_watch_appraise_in", @"");
//            break;
//        case 71:
//            message = FYLocalizedString(@"sleep_watch_appraise_high", @"");
//            break;
//        case 72:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips11", @"");
//            break;
//        case 73:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips13", @"");
//            break;
//        case 74:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips15", @"");
//            break;
//        case 75:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips12", @"");
//            break;
//        case 76:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips14", @"");
//            break;
//        case 77:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips16", @"");
//            break;
//        case 78:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips5", @"");
//            break;
//        case 79:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips7", @"");
//            break;
//        case 80:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips10", @"");
//            break;
//        case 81:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips6", @"");
//            break;
//        case 82:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips8", @"");
//            break;
//        case 83:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips9", @"");
//            break;
//        case 84:
//            message = FYLocalizedString(@"sleep_watch_appraise_tips17", @"");
//            break;
//        case 85:
//            message = @"血氧偏低，请注意";
//            break;
//    }
    
    NSMutableAttributedString *contentAttribute = [[NSMutableAttributedString alloc]initWithString:FYLocalizedString(@"sleep_watch_odi1", @"") attributes:commonDic];
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.1f",hisModel.odi] attributes:keyDic]];
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@/h（%@<10%@/h）\n",FYLocalizedString(@"sleep_watch_odi2", @""),FYLocalizedString(@"sleep_watch_ahi_normal", @""),FYLocalizedString(@"sleep_watch_odi2", @"")] attributes:commonDic]];
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@\n",FYLocalizedString(@"Total number of oxygen desaturation:", @""),@(hisModel.oxygenLowTotal),FYLocalizedString(@"second", @"")] attributes:commonDic]];
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@\n",FYLocalizedString(@"Maximum duration of Low SpO2: ", @""),@(hisModel.oxygenTimeLow),FYLocalizedString(@"s", @"")] attributes:commonDic]];
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@：%.1f%@（%@<1%@）\n",FYLocalizedString(@"sleep_watch_ahi", @""),hisModel.AHI,@"%",FYLocalizedString(@"sleep_watch_ahi_normal", @""),@"%"] attributes:commonDic]];
//    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"最小血氧饱和度：%@%@（正常>90%@）\n",@(hisModel.spo2Min),@"%",@"%"] attributes:commonDic]];
//    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:message attributes:@{NSFontAttributeName:J_MediumFont(14),NSForegroundColorAttributeName:[UIColor blackColor]}]];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 15;
    [contentAttribute addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:contentAttribute.rangeOfAll];
    _detailLabel.attributedText = contentAttribute;
    YYTextContainer  *titleContarer = [YYTextContainer new];
    titleContarer.size = CGSizeMake(self.v_w-30,CGFLOAT_MAX);
    YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:contentAttribute];
    CGSize size = titleLayout.textBoundingSize;
    _detailHeigh.constant = size.height;
    [self layoutIfNeeded];
    
//    self.v_h = _bottomView.v_MaxY+20;
    self.v_h = _detailLabel.v_MaxY+22;
    if (self.heightChangedBlock) {
        self.heightChangedBlock();
    }
}

@end
